<template>
  <nav class="main-nav">
    <div class="nav-container">
      <div class="nav-brand">
        <h1>Page Factory</h1>
        <p>Vue3 Micro Frontend Platform</p>
      </div>
      <div class="nav-stats">
        <div class="stats-card">
          <span class="stats-number">{{ modules.length }}</span>
          <span class="stats-label">Micro Apps</span>
        </div>
        <div class="stats-card">
          <span class="stats-number">{{ activeModules }}</span>
          <span class="stats-label">Active Apps</span>
        </div>
      </div>
      <div class="nav-modules">
        <div class="module-grid">
          <ModuleCard
            v-for="module in modules"
            :key="module.name"
            :module="{
              name: module.meta?.title || module.name,
              path: module.path,
              description: module.meta?.description || 'A micro frontend application',
              isNew: false
            }"
          />
          <ModuleCard
            :module="{
              name: 'Create New App',
              path: '#',
              description: 'Create a new micro frontend application',
              isNew: true
            }"
            @create-module="createNewModule"
          />
        </div>
      </div>
      <div class="nav-footer">
        <p>Built with ❤️ using Vue3 & Micro Frontend Architecture</p>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getModules, registerModule } from '../utils/moduleManager.js'
import ModuleCard from '../components/ModuleCard.vue'

// 动态导入模块配置
const moduleConfigs = []

// 从 modules 目录动态加载模块配置
const modulesContext = import.meta.glob('./modules/*/module.config.js')

Object.keys(modulesContext).forEach(async (path) => {
  try {
    const moduleConfig = await modulesContext[path]()
    moduleConfigs.push(moduleConfig)
  } catch (error) {
    console.warn('Failed to load module config:', path, error)
  }
})

import microFrontendRouter from '../core/MicroFrontendRouter.js'

// 模块列表 - 来自微前端路由系统
const modules = ref([])

// 计算属性：活跃模块数量
const activeModules = computed(() => {
  return modules.value.filter(module => !module.isNew).length
})

function createNewModule() {
  const newModuleName = prompt('Enter new module name:')
  if (newModuleName) {
    registerModule({
      name: newModuleName,
      path: `/${newModuleName.toLowerCase().replace(/\s+/g, '-')}`,
      description: 'A new micro frontend application',
      component: null
    })

    // 重新获取模块列表
    modules.value = getModules()
    alert(`New module "${newModuleName}" created successfully!`)
  }
}

onMounted(async () => {
  // 初始化微前端路由系统
  await microFrontendRouter.initialize()

  // 获取所有路由模块
  modules.value = microFrontendRouter.getRoutes().filter(route => route.name !== 'home')

  console.log('Navigation loaded with', modules.value.length, 'micro apps')

  // 示例：动态注册一些测试模块
  if (modules.value.length === 0) {
    registerModule({
      name: 'Demo App',
      path: '/demo-app',
      description: 'A demonstration micro frontend',
      component: null
    })

    const addedModules = getModules()
    modules.value = [...modules.value, ...addedModules]

    console.log('Added demo modules, total:', modules.value.length)
  }
})
</script>

<style scoped>
.main-nav {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  padding: 20px;
  color: white;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
}

.nav-brand {
  text-align: center;
  margin-bottom: 40px;
}

.nav-brand h1 {
  color: white;
  font-size: 2.5em;
  margin: 0;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.nav-brand p {
  margin: 10px 0 0 0;
  opacity: 0.9;
  font-size: 1.1em;
}

.nav-stats {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-bottom: 40px;
}

.stats-card {
  background: rgba(255, 255, 255, 0.1);
  padding: 15px 25px;
  border-radius: 10px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.stats-number {
  display: block;
  font-size: 2em;
  font-weight: bold;
  margin-bottom: 5px;
}

.stats-label {
  font-size: 0.9em;
  opacity: 0.9;
}

.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.nav-footer {
  text-align: center;
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  opacity: 0.8;
}

@media (max-width: 768px) {
  .nav-stats {
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }

  .module-grid {
    grid-template-columns: 1fr;
    padding: 0 20px;
  }

  .nav-brand h1 {
    font-size: 2em;
  }

  .stats-card {
    min-width: 150px;
  }
}
</style>